package com.jake.microservice.provider.mybatis.controller;

import com.jake.microservice.model.UserDTO;
import com.jake.microservice.model.UserInfoDTO;
import com.jake.microservice.provider.mybatis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("users")
@Validated
@Slf4j
public abstract class MyBatisUserController {

    @Autowired
    private UserService userService;

    /**
     * 查找所有用户
     * @return
     */
    @GetMapping
    public List<UserDTO> findAllUsers() {
        return userService.findAllUsers();
    }

    /**
     * 查找所有用户信息
     * @return
     */
    @GetMapping("info")
    public List<UserDTO> findAllUserInfo() {
        return userService.findAllUserInfo();
    }

    /**
     * 通过id查找用户
     * @param uid
     * @return
     */
    @GetMapping("{uid}")
    public UserDTO findUserById(@PathVariable Integer uid) {
        log.info(getClass().toString());
        return userService.findUserById(uid);
    }

    /**
     * 通过id查找用户信息
     * @param uid
     * @return
     */
    @GetMapping("info/{uid}")
    public UserInfoDTO findUserInfoById(@PathVariable Integer uid) {
        return userService.findUserInfoById(uid);
    }

    /**
     * 通过用户名和公司名查找用户
     * @param username
     * @param cname
     * @return
     */
    @GetMapping("info/{cname}/{username}")
    public List<UserInfoDTO> findUserInfoByUsernameAndCname(@PathVariable String username, @PathVariable String cname) {
        return userService.findUserInfoByUsernameAndCname(username, cname);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping
    public int createUser(@RequestBody @NotNull UserDTO user) {
        return userService.createUser(user);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping("{uid}")
    public int updateUser(@RequestBody @NotNull UserDTO user, @PathVariable @NotNull Integer uid) {
        UserDTO userInDb = userService.findUserById(uid);
        if (userInDb == null) {
            return 0;
        }
        if (user.getUid() == null) {
            user.setUid(uid);
        }
        if (uid.equals(user.getUid())) {
            return userService.updateUser(user);
        } else {
            log.error("传入User对象中的uid和PathVariable中的uid不等，不予更新");
            return 0;
        }
    }

    /**
     * 修改用户姓名
     * @param username
     * @return
     */
    @PatchMapping("{uid}")
    public int updateUsername(@NotNull String username, @PathVariable @NotNull Integer uid) {
        UserDTO userInDb = userService.findUserById(uid);
        if (userInDb == null) {
            return 0;
        }
        return userService.updateUsername(username, uid);
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @DeleteMapping("{uid}")
    public int removeUserById(@PathVariable Integer uid) {
        UserDTO user = userService.findUserById(uid);
        if (user == null) {
            log.error("数据库中不存在id为{}的用户", uid);
            return 0;
        } else {
            return userService.removeUserById(uid);
        }
    }

}
